package htmlcli

import (
	"fmt"
	"log"
	"net/http"
)

type FileServer struct {
	fs http.Handler
}

func NewFileServer(dir string, port int) *FileServer {
	ws := FileServer{fs: http.FileServer(http.Dir(dir))}
	go func() {
		err := http.ListenAndServe(fmt.Sprintf(":%d", port), &ws)
		if err != nil {
			log.Println(err, "直接使用端口")
		}
	}()
	return &ws
}

func (ws *FileServer) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
	rw.Header().Set("Access-Control-Allow-Origin", "*")             //允许访问所有域
	rw.Header().Add("Access-Control-Allow-Headers", "Content-Type") //header 的类型
	ws.fs.ServeHTTP(rw, r)
}
